﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.Revit.MEP.Calculator
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdHideRoomTag : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(typeof(FamilyInstance));
			if (filteredElementCollector != null)
			{
				Transaction transaction = new Transaction(document, "Hide");
				foreach (Element element in filteredElementCollector.ToElements())
				{
					AnnotationSymbol annotationSymbol = element as AnnotationSymbol;
					if (annotationSymbol != null && annotationSymbol.Symbol.Family.Name == "房间照明标记")
					{
						ElementSet elementSet = new ElementSet();
						elementSet.Insert(element);
						Autodesk.Revit.DB.View view = document.GetElement(element.OwnerViewId) as Autodesk.Revit.DB.View;
						transaction.Start();
						try
						{
							view.HideYJKElements(elementSet);
							transaction.Commit();
						}
						catch
						{
							transaction.RollBack();
						}
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
